Analyze selection data using soluble Ephrin-B2 or -B3¶

In [1]:
# this cell is tagged as parameters for `papermill` parameterization
#input configs
altair_config = None
nipah_config = None

#input files
entropy_file = None
func_scores_E2_file = None
binding_E2_file = None
func_scores_E3_file = None
binding_E3_file = None

#output files
filtered_E2_binding_data = None
filtered_E3_binding_data = None
filtered_E2_binding_low_effect = None
filtered_E3_binding_low_effect = None

#output images
entry_binding_combined_corr_plot = None
entry_binding_combined_corr_plot_agg = None
E2_E3_correlation = None
E2_E3_correlation_site = None
combined_E2_E3_site_corr = None
binding_by_site_plot = None
entry_binding_corr_heatmap = None
binding_corr_heatmap = None
binding_region_boxplot_plot = None
binding_region_bubble_plot = None
max_binding_in_stalk = None
max_binding_in_contact = None
In [2]:
# Parameters
nipah_config = "nipah_config.yaml"
altair_config = "data/custom_analyses_data/theme.py"
entropy_file = "results/entropy/entropy.csv"
func_scores_E2_file = "results/func_effects/averages/CHO_EFNB2_low_func_effects.csv"
binding_E2_file = "results/receptor_affinity/averages/EFNB2_monomeric_mut_effect.csv"
func_scores_E3_file = "results/func_effects/averages/CHO_EFNB3_low_func_effects.csv"
binding_E3_file = "results/receptor_affinity/averages/EFNB3_dimeric_mut_effect.csv"
filtered_E2_binding_data = "results/filtered_data/E2_binding_filtered.csv"
filtered_E3_binding_data = "results/filtered_data/E3_binding_filtered.csv"
filtered_E2_binding_low_effect = (
    "results/filtered_data/E2_binding_low_effect_filter.csv"
)
filtered_E3_binding_low_effect = (
    "results/filtered_data/E3_binding_low_effect_filter.csv"
)
entry_binding_combined_corr_plot = (
    "results/images/entry_binding_combined_corr_plot.html"
)
entry_binding_combined_corr_plot_agg = (
    "results/images/entry_binding_combined_corr_plot_agg.html"
)
E2_E3_correlation = "results/images/E2_E3_correlation.html"
E2_E3_correlation_site = "results/images/E2_E3_correlation_site.html"
combined_E2_E3_site_corr = "results/images/combined_E2_E3_site_corr.html"
binding_by_site_plot = "results/images/binding_by_site_plot.html"
entry_binding_corr_heatmap = "results/images/entry_binding_corr_heatmap.html"
binding_corr_heatmap = "results/images/binding_corr_heatmap.html"
binding_region_boxplot_plot = "results/images/binding_region_boxplot_plot.html"
binding_region_bubble_plot = "results/images/binding_region_bubble_plot.html"
max_binding_in_contact = "results/images/max_binding_in_contact.html"
max_binding_in_stalk = "results/images/max_binding_in_stalk.html"
In [3]:
import math
import os
import re
import altair as alt
import numpy as np
import pandas as pd
import scipy.stats
import yaml
In [4]:
# allow more rows for Altair
_ = alt.data_transformers.disable_max_rows()

if os.getcwd() == '/fh/fast/bloom_j/computational_notebooks/blarsen/2023/Nipah_Malaysia_RBP_DMS/':
    pass
    print("Already in correct directory")
else:
    os.chdir("/fh/fast/bloom_j/computational_notebooks/blarsen/2023/Nipah_Malaysia_RBP_DMS/")
    print("Setup in correct directory")
Setup in correct directory
In [5]:
if nipah_config is None:
##hard paths in case don't want to run with snakemake
    print('loading hard paths')
    altair_config = "data/custom_analyses_data/theme.py"
    nipah_config = "nipah_config.yaml"
    entropy_file = 'results/entropy/entropy.csv'
    
    #input files
    func_scores_E2_file = "results/func_effects/averages/CHO_EFNB2_low_func_effects.csv"
    binding_E2_file = "results/receptor_affinity/averages/EFNB2_monomeric_mut_effect.csv"
    func_scores_E3_file = "results/func_effects/averages/CHO_EFNB3_low_func_effects.csv"
    binding_E3_file = "results/receptor_affinity/averages/EFNB3_dimeric_mut_effect.csv"

    filtered_E2_binding_data="results/filtered_data/E2_binding_filtered.csv"
    filtered_E3_binding_data="results/filtered_data/E3_binding_filtered.csv"
    filtered_E2_binding_low_effect="results/filtered_data/E2_binding_low_effect_filter.csv"
    filtered_E3_binding_low_effect="results/filtered_data/E3_binding_low_effect_filter.csv"

Run config files to setup altair theme and config variables¶

In [6]:
if altair_config:
    with open(altair_config, 'r') as file:
        exec(file.read())

with open(nipah_config) as f:
    config = yaml.safe_load(f)

Make the E2/E3 dataframes, filter separately, then merge¶

In [7]:
#import binding and entry data
e2 = pd.read_csv(binding_E2_file)
e2_func = pd.read_csv(func_scores_E2_file)
e3 = pd.read_csv(binding_E3_file)
e3_func = pd.read_csv(func_scores_E3_file)

Filter the data and save¶

In [8]:
def merge_func_binding_dfs(func,binding,name):
    df_int = pd.merge(
        binding,
        func,
        on=['site','mutant','wildtype'],
        suffixes=['_binding','_cell_entry'],
        validate='one_to_one',
        how='outer'
    ).round(3)
    df = df_int.rename(columns={'Ephrin binding_mean':'binding_mean','Ephrin binding_std':'binding_std','Ephrin binding_median':'binding_median'})

    # Only save relevant columns
    df = df[['site','wildtype','mutant','binding_median','binding_std','times_seen_binding','effect','effect_std','times_seen_cell_entry','frac_models']]
    
    def filter_binding_data(df):
        df_filter = df[
            (df['mutant'] != '*') &
            (df['mutant'] != '-') &
            (df['site'] != 603) &
            # Filter cell entry parameters
            (df['effect'] >= config['min_func_effect_for_binding']) &
            (df['times_seen_cell_entry'] >= config['func_times_seen_cutoff']) &
            (df['effect_std'] <= config['func_std_cutoff']) &
            # Filter binding parameters
            (df['times_seen_binding'] >= config['min_times_seen_binding']) &
            (df['binding_std'] <= config['max_binding_std']) &
            (df['frac_models'] >= config['frac_models'])
        ]
        return df_filter

    df_filter = filter_binding_data(df)
    
    #For pulling out low effect mutants for heatmaps later. Find mutants below func effect cutoff, but still have ok times_seen and func_std.
    def store_filtered_info(df):
        df_low_filter = df[
            (df['mutant'] != '*') &
            (df['mutant'] != '-') &
            (df['site'] != 603) &
            (df['effect'] < config['min_func_effect_for_binding']) &
            (df['times_seen_cell_entry'] >= config['func_times_seen_cutoff']) &
            (df['effect_std'] <= config['func_std_cutoff']) 
        ]
        return df_low_filter
    
    df_low_effect_filter = store_filtered_info(df)
    
    if name == 'EFNB2':
        print(name)
        df_filter.to_csv(filtered_E2_binding_data,index=False)
        df_low_effect_filter.to_csv(filtered_E2_binding_low_effect,index=False)
    else:
        df_filter.to_csv(filtered_E3_binding_data,index=False)
        df_low_effect_filter.to_csv(filtered_E3_binding_low_effect,index=False)
    
    return df_filter,df_low_effect_filter

#Call filtering function
df_E2_filter,df_E2_filter_missing = merge_func_binding_dfs(e2_func,e2,'EFNB2')
df_E3_filter,df_E3_filter_missing = merge_func_binding_dfs(e3_func,e3,'EFNB3')

#Now that they are filtered, merge EFNB2 and EFNB3
df_binding_effect_merge = pd.merge(
    df_E2_filter,
    df_E3_filter,
    on=['site','wildtype','mutant'],
    suffixes=['_E2','_E3'],
    how='outer'
)

#display stats
display(df_binding_effect_merge.describe().round(3))

# Make a concat df of E2/E3 data for plotting later
df_E2_filter['selection'] = 'EFNB2'
df_E3_filter['selection'] = 'EFNB3'
df_binding_effect_concat = pd.concat([df_E2_filter,df_E3_filter])
EFNB2
site binding_median_E2 binding_std_E2 times_seen_binding_E2 effect_E2 effect_std_E2 times_seen_cell_entry_E2 frac_models_E2 binding_median_E3 binding_std_E3 times_seen_binding_E3 effect_E3 effect_std_E3 times_seen_cell_entry_E3 frac_models_E3
count 7212.000 6684.000 6684.000 6684.000 6684.000 6684.000 6684.000 6684.000 6519.000 6519.000 6519.000 6519.000 6519.000 6519.000 6519.0
mean 343.684 -0.333 0.501 6.400 -0.061 0.368 7.699 0.998 -0.017 0.179 6.227 -0.039 0.388 6.804 1.0
std 148.183 1.095 0.321 3.203 0.495 0.191 4.383 0.022 0.271 0.169 3.053 0.474 0.186 3.584 0.0
min 71.000 -5.494 0.008 2.250 -1.500 0.010 2.000 0.750 -2.147 0.000 2.500 -1.500 0.033 2.000 1.0
25% 217.000 -0.416 0.273 4.500 -0.335 0.223 5.000 1.000 -0.150 0.062 4.500 -0.266 0.248 4.714 1.0
50% 342.000 -0.033 0.426 5.750 0.076 0.337 6.750 1.000 -0.012 0.135 5.500 0.094 0.356 6.000 1.0
75% 468.000 0.190 0.645 7.500 0.329 0.480 9.125 1.000 0.118 0.241 7.500 0.324 0.496 7.857 1.0
max 602.000 2.205 1.995 43.750 0.617 0.994 72.380 1.000 2.006 1.780 45.000 0.616 1.000 56.710 1.0

Make nice interactive plot for correlation between binding and entry for EFNB2 and EFNB3¶

In [9]:
def plot_corr_binding_entry_updated(df,flag):
    variant_selector = alt.selection_point(
        on="mouseover",
        empty=False,
        fields=["site","mutant"],
        value=0
    )  
    variant_selector_agg = alt.selection_point(
        on="mouseover",
        empty=False,
        fields=["site"],
        value=0
    )  
    slider = alt.binding_range(min=2, max=10, step=1, name="times seen")
    selector = alt.param(name="SelectorName", value=2, bind=slider)

    empty_chart = []
    for cell in list(df['selection'].unique()):
        tmp_df = df[df['selection'] == cell]
        if flag == True:
            agg_df = tmp_df.groupby('site')[['binding_median','effect']].sum().reset_index()
            chart = alt.Chart(agg_df).mark_point(stroke='black',filled=True).encode(
                x=alt.X('effect', title=f'Summed {cell} Cell Entry', axis=alt.Axis(grid=True)),
                y=alt.Y('binding_median', title=f'Summed {cell} Binding', axis=alt.Axis(grid=True)),
                opacity=alt.condition(variant_selector_agg, alt.value(1), alt.value(0.2)),
                size=alt.condition(variant_selector_agg,alt.value(100),alt.value(50)),
                strokeWidth=alt.condition(variant_selector_agg,alt.value(1),alt.value(0)),
                color=alt.condition(variant_selector_agg,alt.value('orange'),alt.value('black')),
                tooltip=['site', 'binding_median','effect'],
            ).add_params(variant_selector_agg)
            
            empty_chart.append(chart)
        
        
        else:
            chart = alt.Chart(tmp_df).mark_point(stroke='black',filled=True).encode(
                x=alt.X('effect', title=f'{cell} Cell Entry', axis=alt.Axis(grid=True)),
                y=alt.Y('binding_median', title=f'{cell} Binding', axis=alt.Axis(grid=True)),
                opacity=alt.condition(variant_selector, alt.value(1), alt.value(0.1)),
                size=alt.condition(variant_selector,alt.value(50),alt.value(20)),
                strokeWidth=alt.condition(variant_selector,alt.value(1),alt.value(0)),
                color=alt.condition(variant_selector,alt.value('orange'),alt.value('black')),
                tooltip=['site', 'wildtype', 'mutant','binding_median','times_seen_binding','effect'],
            ).add_params(variant_selector)
            empty_chart.append(chart)
    
    combined_chart = alt.hconcat(*empty_chart,title=alt.Title('Correlation between binding and entry'))
    return combined_chart

entry_binding_corr_plot = plot_corr_binding_entry_updated(df_binding_effect_concat,False)
entry_binding_corr_plot.display()
if entry_binding_combined_corr_plot is not None:
    entry_binding_corr_plot.save(entry_binding_combined_corr_plot)

entry_binding_corr_plot_agg = plot_corr_binding_entry_updated(df_binding_effect_concat,True)
entry_binding_corr_plot_agg.display()
if entry_binding_combined_corr_plot is not None:
    entry_binding_corr_plot_agg.save(entry_binding_combined_corr_plot_agg)
In [10]:
def plot_entry_binding_corr_heatmap(df):
    empty_chart = []
    
    for cell in list(df['selection'].unique()):
        tmp_df = df[df['selection'] == cell]
        chart = alt.Chart(tmp_df,title=f'{cell}').mark_rect().encode(
            x=alt.X('effect',title='Cell Entry',axis=alt.Axis(values=[-2,-1,0,1])).bin(maxbins=60),
            y=alt.Y('binding_median',title='Binding',axis=alt.Axis(values=[-4,-2,0,2])).bin(maxbins=60),
            color=alt.Color('count()',title='Count').scale(scheme='greenblue'),
        )
        empty_chart.append(chart)
    
    combined_chart = alt.hconcat(*empty_chart,title=alt.Title('Correlation between binding and entry')).resolve_scale(y='shared',x='shared',color='shared')
    return combined_chart

entry_binding_corr_heat = plot_entry_binding_corr_heatmap(df_binding_effect_concat)
entry_binding_corr_heat.display()
if entry_binding_combined_corr_plot is not None:
    entry_binding_corr_heat.save(entry_binding_corr_heatmap)
In [11]:
def overall_stats(df,effect,name):
    #Find quantiles
    quantile_2 = df['binding_median'].quantile(.02)
    quantile_98 = df['binding_median'].quantile(.98)
    print(f'The 2% quantile for {name} is: {quantile_2}')
    print(f'The 98% quantile for {name} is: {quantile_98}')

    #Now group sites and find intolerant sites 
    filtered_df = df.groupby('site').filter(lambda group: (group[effect] <-0.25).all())
    unique = filtered_df['site'].unique()
    # Convert unique to a Pandas Series
    unique_series = pd.Series(unique)
    # Find the common elements
    unique_contact_bool = unique_series.isin(config['contact_sites'])
    # Filter and get the common elements
    common_elements = unique_series[unique_contact_bool]

    # Print the common elements
    print(f'Here are the contact sites that are conserved: {common_elements}')
    
    print(f'There are {len(unique)} sites with all negative binding score mutants for {name}')
    print(f'These are the sites for {name} with all negative binding score mutants: {list(unique)}')

    #Now find sites with low and high binding (median)
    median_df = df.groupby('site')['binding_median'].median().reset_index().sort_values(by='binding_median')
    print(f'For {name}, these are the sites with lowest median binding scores: {median_df.head(5)}')
    median_df = df.groupby('site')['binding_median'].median().reset_index().sort_values(by='binding_median',ascending=False)
    print(f'For {name}, these are the sites with highest median binding scores: {median_df.head(5)}')
    
    #Now calculate mutant number
    total_mutants = df.shape[0]
    upper_cutoff = df[df[effect] > 1].sort_values(by='binding_median',ascending=False)
    median_upper = upper_cutoff['effect'].median()
    print(f'The median entry score for top binders was: {median_upper}')
    
    mutants_above_cutoff_tolerated = upper_cutoff[upper_cutoff['effect'] > 0]
    mutants_above_cutoff_tolerated = mutants_above_cutoff_tolerated[['site','effect','binding_median','wildtype','mutant']]
    print(f'The mutants with positive entry scores and good binding are: {mutants_above_cutoff_tolerated.head(5)}')
    
    lower_cutoff = df[df[effect] < -1]
    
    print(f'For {name}, there are a total of : {total_mutants} binding mutants')
    print(f'For {name}, there are {upper_cutoff.shape[0]} mutants above cutoff, and {mutants_above_cutoff_tolerated.shape[0]} that have good entry scores')
    print(f'For {name}, there are {lower_cutoff.shape[0]} mutants below cutoff')
 
    total_sites = df['site'].unique().shape[0]
    
    print(f'The total number of sites are: {total_sites}')


overall_stats(df_E2_filter,'binding_median','E2')
overall_stats(df_E3_filter,'binding_median','E3')
The 2% quantile for E2 is: -3.95034
The 98% quantile for E2 is: 1.1267400000000016
Here are the contact sites that are conserved: 3     238
4     239
5     242
11    389
23    488
24    490
25    491
29    501
30    504
31    505
33    531
34    532
35    533
36    557
37    579
38    581
41    588
dtype: int64
There are 43 sites with all negative binding score mutants for E2
These are the sites for E2 with all negative binding score mutants: [116, 220, 236, 238, 239, 242, 243, 248, 346, 351, 352, 389, 390, 398, 399, 400, 435, 438, 441, 460, 467, 486, 487, 488, 490, 491, 494, 495, 497, 501, 504, 505, 526, 531, 532, 533, 557, 579, 581, 584, 585, 588, 590]
For E2, these are the sites with lowest median binding scores:      site  binding_median
442   533         -4.1470
403   490         -4.1095
406   494         -4.0490
439   530         -3.8740
466   557         -3.8670
For E2, these are the sites with highest median binding scores:      site  binding_median
131   207           1.511
179   259           1.448
33    104           1.358
132   208           1.340
48    120           1.329
The median entry score for top binders was: -0.7475
The mutants with positive entry scores and good binding are:        site  effect  binding_median wildtype mutant
9548    566   0.186           1.388        F      H
7303    450   0.316           1.330        Q      I
10034   591   0.027           1.266        K      G
4540    306   0.110           1.239        N      T
9513    564   0.228           1.196        N      K
For E2, there are a total of : 6684 binding mutants
For E2, there are 190 mutants above cutoff, and 18 that have good entry scores
For E2, there are 982 mutants below cutoff
The total number of sites are: 510
The 2% quantile for E3 is: -0.629
The 98% quantile for E3 is: 0.6
Here are the contact sites that are conserved: 3     389
6     488
10    501
12    531
13    532
dtype: int64
There are 15 sites with all negative binding score mutants for E3
These are the sites for E3 with all negative binding score mutants: [108, 140, 352, 389, 467, 486, 488, 494, 495, 497, 501, 510, 531, 532, 584]
For E3, these are the sites with lowest median binding scores:      site  binding_median
411   501          -0.907
437   531          -0.742
271   352          -0.697
305   389          -0.684
460   555          -0.629
For E3, these are the sites with highest median binding scores:      site  binding_median
490   589          0.6230
56    129          0.5280
86    161          0.4840
185   266          0.4735
59    132          0.4675
The median entry score for top binders was: -0.7805
The mutants with positive entry scores and good binding are:        site  effect  binding_median wildtype mutant
8010    492   0.548           1.261        Q      L
2637    211   0.436           1.169        G      F
10031   598   0.422           1.154        P      G
For E3, there are a total of : 6519 binding mutants
For E3, there are 24 mutants above cutoff, and 3 that have good entry scores
For E3, there are 14 mutants below cutoff
The total number of sites are: 504

Find sites with opposite effects on binding¶

In [12]:
#find sites that are different
def find_biggest_differences(df):
    efnb2_good_efnb3_bad = df[
        (df['binding_median_E2'] > 0.1) &
        (df['binding_median_E3'] < -0.1)
    ].sort_values(by='binding_median_E2',ascending=False)
    display(efnb2_good_efnb3_bad)
    
    efnb2_bad_efnb3_good = df[
        (df['binding_median_E2'] < -0.1) &
        (df['binding_median_E3'] > 0.1)
    ].sort_values(by='binding_median_E3',ascending=False)
    display(efnb2_bad_efnb3_good)

find_biggest_differences(df_binding_effect_merge)
site wildtype mutant binding_median_E2 binding_std_E2 times_seen_binding_E2 effect_E2 effect_std_E2 times_seen_cell_entry_E2 frac_models_E2 binding_median_E3 binding_std_E3 times_seen_binding_E3 effect_E3 effect_std_E3 times_seen_cell_entry_E3 frac_models_E3
437 117 T V 1.680 1.195 6.50 -0.921 0.747 8.875 1.0 -0.232 0.296 6.5 -0.441 0.609 6.286 1.0
456 120 I M 1.329 0.783 5.50 -0.656 0.452 6.750 1.0 -0.362 0.811 4.0 -0.037 0.383 5.714 1.0
3971 397 L Q 1.299 0.288 3.50 -0.588 0.731 3.500 1.0 -0.122 0.317 4.0 0.323 0.585 2.429 1.0
867 170 E T 1.289 0.865 4.25 -0.547 0.701 4.250 1.0 -0.137 0.242 4.0 -0.609 0.662 5.000 1.0
6551 591 K G 1.266 0.156 5.75 0.027 0.442 6.125 1.0 -0.115 0.151 6.0 -0.069 0.569 6.286 1.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1954 251 G L 0.104 0.313 4.25 -0.956 0.706 3.625 1.0 -0.159 0.064 4.0 -1.390 0.387 4.429 1.0
1133 186 N H 0.103 0.270 4.75 0.331 0.521 6.750 1.0 -0.315 0.115 4.0 -0.053 0.189 3.714 1.0
5594 519 W Q 0.102 0.354 5.00 0.498 0.215 6.250 1.0 -0.151 0.050 5.0 0.343 0.913 4.000 1.0
4671 446 D C 0.101 0.226 3.25 0.438 0.136 3.375 1.0 -0.243 0.116 3.0 0.542 0.205 3.000 1.0
5598 519 W V 0.101 0.348 5.25 0.225 0.251 4.750 1.0 -0.138 0.165 6.5 0.452 0.169 5.143 1.0

356 rows × 17 columns

site wildtype mutant binding_median_E2 binding_std_E2 times_seen_binding_E2 effect_E2 effect_std_E2 times_seen_cell_entry_E2 frac_models_E2 binding_median_E3 binding_std_E3 times_seen_binding_E3 effect_E3 effect_std_E3 times_seen_cell_entry_E3 frac_models_E3
5284 492 Q L -0.186 0.259 5.50 0.542 0.293 4.500 1.0 1.261 0.067 5.5 0.548 0.183 5.143 1.0
6512 588 I P -2.042 0.840 5.25 -0.276 0.300 4.750 1.0 1.183 0.255 5.5 -0.607 0.382 5.429 1.0
6614 598 P G -0.228 0.785 5.00 -1.092 0.939 4.625 1.0 1.154 1.559 4.5 0.422 0.515 5.429 1.0
462 123 N G -0.228 0.658 3.25 -0.974 0.417 4.375 1.0 0.809 0.157 2.5 -0.142 0.475 4.286 1.0
557 137 S D -0.254 0.838 4.00 -1.127 0.335 7.000 1.0 0.804 0.831 4.0 -0.238 0.760 3.571 1.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
5561 517 I W -0.258 0.376 4.75 0.299 0.079 7.375 1.0 0.103 0.068 5.5 0.457 0.159 5.286 1.0
6251 569 K R -0.510 0.554 7.25 0.468 0.114 7.625 1.0 0.102 0.089 6.5 0.142 0.432 6.143 1.0
3556 370 L Q -0.266 1.169 4.75 -0.156 0.527 4.375 1.0 0.102 0.095 4.5 0.363 0.177 5.429 1.0
2599 306 N E -0.170 0.824 2.50 0.124 0.433 2.375 1.0 0.101 0.158 2.5 0.503 0.140 2.571 1.0
4163 410 R Q -0.328 0.140 8.00 0.384 0.234 8.375 1.0 0.101 0.081 8.0 0.087 0.362 8.143 1.0

224 rows × 17 columns

Find correlations between EFNB2 and EFNB3 binding¶

In [13]:
def plot_entry_binding_corr(df):
    chart = alt.Chart(df,title='Correlation Between Mutant Binding Scores').mark_rect().encode(
        x=alt.X('binding_median_E2',title='EFNB2 binding',axis=alt.Axis(values=[-5,0,2])).bin(maxbins=40),
        y=alt.Y('binding_median_E3',title='EFNB3 binding',axis=alt.Axis(values=[-2,0,2])).bin(maxbins=40),
        color=alt.Color('count()',title='Count').scale(scheme='greenblue'),
    )
    return chart

entry_binding_corr_heatmap_1 = plot_entry_binding_corr(df_binding_effect_merge)
entry_binding_corr_heatmap_1.display()
if entry_binding_combined_corr_plot is not None:
    entry_binding_corr_heatmap_1.save(binding_corr_heatmap)
In [14]:
def plot_affinity_solid(df):
    df = df.dropna()
    # calculate r value
    slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(df['binding_median_E2'], df['binding_median_E3'])
    r_value = float(r_value)
    # make chart
    chart = alt.Chart(df,title=alt.Title('Correlation between Mutant Binding Scores',subtitle=f'r={r_value:.2f}')).mark_point(color='black',size=30, opacity=0.2,filled=True).encode(
        x=alt.X('binding_median_E2', title=('EFNB2 Binding')),
        y=alt.Y('binding_median_E3', title=('EFNB3 Binding')),
        tooltip=['site', 'wildtype','mutant','binding_median_E2','binding_median_E3','effect_E2','effect_E3'],
    )
    min = int(df['binding_median_E2'].min())
    max = int(df['binding_median_E3'].max())
    text = alt.Chart({'values':[{'x': min, 'y': max, 'text': f'r = {r_value:.2f}'}]}).mark_text(
        align='left', baseline='top', dx=-10, dy=-20).encode(
            x=alt.X('x:Q'),
            y=alt.Y('y:Q'),
            text='text:N'
        )
    chart_and_text = chart
    return chart_and_text

E2_E3_corr = plot_affinity_solid(df_binding_effect_merge)
E2_E3_corr.display()
if entry_binding_combined_corr_plot is not None:
    E2_E3_corr.save(E2_E3_correlation)

Plot correlations between summary statistics for each site¶

In [15]:
def plot_affinity_solid_mean(df):
    df = df.dropna()
    means = df.groupby('site').agg({
            'effect_E2': 'median',
            'effect_E3': 'median',
            'binding_median_E2': 'median',
            'binding_median_E3': 'median',
            'wildtype': 'first'
        }).reset_index()
    slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(means['binding_median_E2'], means['binding_median_E3'])
    r_value = float(r_value)
    chart = alt.Chart(means,title=alt.Title('Correlation between Aggregate Mutant Binding Scores',subtitle=f'r={r_value:.2f}')).mark_point(size=50, opacity=0.3).encode(
            x=alt.X('binding_median_E2', title=('Median EFNB2 Binding'), axis=alt.Axis(tickCount=3)),
            y=alt.Y('binding_median_E3', title=('Median EFNB3 Binding'), axis=alt.Axis(tickCount=3)),
            tooltip=['site', 'wildtype','binding_median_E2','binding_median_E3','effect_E2','effect_E3'],
        )
    text = alt.Chart({'values':[{'x': -3.5, 'y': 0.5, 'text': f'r = {r_value:.2f}'}]}).mark_text(
        align='left', baseline='top', dx=0, dy=-10).encode(
            x=alt.X('x:Q'),
            y=alt.Y('y:Q'),
            text='text:N'
        )
    chart_and_text = chart 
    return chart_and_text

E2_E3_site_corr = plot_affinity_solid_mean(df_binding_effect_merge)
E2_E3_site_corr.display()
if entry_binding_combined_corr_plot is not None:
    E2_E3_site_corr.save(E2_E3_correlation_site)

if entry_binding_combined_corr_plot is not None:
    (E2_E3_site_corr | E2_E3_corr).save(combined_E2_E3_site_corr)

Make plot showing binding by site (median)¶

In [16]:
def plot_affinity_by_site_median(df):
    variant_selector = alt.selection_point(
        on="mouseover",
        nearest=True,
        empty=False,
        fields=["site"],
        value=0
    )  
    empty_charts = []
    for selection in ['binding_median_E2','binding_median_E3']:
        if selection == 'binding_median_E2':
            name = 'EFNB2 Binding'
        else:
            name = 'EFNB3 Binding'
        mean = df.groupby('site')[selection].max().reset_index()
        mean = mean[mean[selection] >= 0]
        chart = alt.Chart(mean).mark_point(stroke='black',filled=True,size=50).encode(
            x=alt.X('site', title=('Site'), axis=alt.Axis(grid=True, tickCount=4),scale=alt.Scale(domain=[70,602])),
            y=alt.Y(selection, title=(name), axis=alt.Axis(grid=True, tickCount=3)),
            tooltip=['site'],
            color=alt.condition(variant_selector, alt.value('orange'), alt.value('black')),
            opacity=alt.condition(variant_selector, alt.value(1), alt.value(0.5)),
            strokeWidth=alt.condition(variant_selector,alt.value(1),alt.value(0))
        ).properties(height=150,width=500).add_params(variant_selector)
        empty_charts.append(chart)
    combined_chart = alt.vconcat(*empty_charts, spacing=1,title='Max Binding by Site')
    return combined_chart



binding_by_site = plot_affinity_by_site_median(df_binding_effect_merge)
binding_by_site.display()
if entry_binding_combined_corr_plot is not None:
    binding_by_site.save(binding_by_site_plot)
In [17]:
def plot_affinity_by_contact_site(df,sites_to_show,title_text):
    variant_selector = alt.selection_point(
        on="mouseover",
        nearest=True,
        empty=False,
        fields=["site"],
        value=0
    )  
    empty_charts = []

    contact_df = df[df['site'].isin(sites_to_show)]
    sites = list(contact_df['site'].unique())
    
    for selection in df['selection'].unique():
        tmp_df = contact_df[contact_df['selection'] == selection]
        mean = tmp_df.groupby('site')['binding_median'].max().reset_index()
        
        chart = alt.Chart(mean).mark_point(size=100).encode(
            x=alt.X('site:O', sort=sites,title=('Site'), axis=alt.Axis(grid=True, labelAngle=-90),scale=alt.Scale(domain=sites)),
            y=alt.Y('binding_median', title=(f'{selection}'), axis=alt.Axis(grid=True)),
            tooltip=['site'],
            color=alt.condition(variant_selector, alt.value('orange'), alt.value('black')),
            strokeWidth=alt.condition(variant_selector,alt.value(2),alt.value(0))
        ).add_params(variant_selector)
        empty_charts.append(chart)
    combined_chart = alt.vconcat(*empty_charts, spacing=1,title=title_text)
    return combined_chart



contact_binding_by_site = plot_affinity_by_contact_site(df_binding_effect_concat,config['contact_sites'],'Max Binding in Contact')
contact_binding_by_site.display()
if entry_binding_combined_corr_plot is not None:
    contact_binding_by_site.save(max_binding_in_contact)

contact_binding_by_site_stalk = plot_affinity_by_contact_site(df_binding_effect_concat,list(range(96, 147)),"Max Binding in Stalk")
contact_binding_by_site_stalk.display()
if entry_binding_combined_corr_plot is not None:
    contact_binding_by_site_stalk.save(max_binding_in_stalk)

Make bubble plots for binding in different areas of receptor pocket¶

In [18]:
def make_boxplot_binding_region(df,title):# Create a box plot using Altair for aggregated means
    barrel_ranges = {
    'Hydrophobic': config['hydrophobic'],
    'Salt Bridges': config['salt_bridges'],
    'Hydrogen Bonds': config['h_bond_total'],
    'Contact': config['contact_sites'],
    'Overall': list(range(71,602)),
    }
    
    mean_df = df.groupby('site')[['binding_median']].median().reset_index()
    custom_order = ['Hydrophobic','Salt Bridges','Hydrogen Bonds','Contact','Overall']
    agg_means = []
    
    # For each barrel, filter the site_means dataframe to the sites belonging to that barrel and then store the means
    for barrel, sites in barrel_ranges.items():
        subset = mean_df[mean_df['site'].isin(sites)]
        for _, row in subset.iterrows():
            agg_means.append({'barrel': barrel, 'effect': row['binding_median'],'site':row['site']})
        agg_means_df = pd.DataFrame(agg_means)
    chart = alt.Chart(agg_means_df).mark_point(size=50,opacity=0.4).encode(
                x=alt.X('barrel:O', sort=custom_order,title=None,axis=alt.Axis(labelAngle=-90)),
                y=alt.Y('effect',title=f'Median {title} Binding',axis=alt.Axis(grid=True,tickCount=4)),
                xOffset='random:Q',
                tooltip=['barrel', 'effect','site'],
            ).transform_calculate(
                random="sqrt(-1*log(random()))*cos(2*PI*random())"
        
            )
    
    return chart.display()

make_boxplot_binding_region(df_E2_filter,'EFNB2')
make_boxplot_binding_region(df_E3_filter,'EFNB3')

make boxplot of binding scores by region¶

In [19]:
def make_boxplot_binding_region(df):
    barrel_ranges = {
        "Stalk": list(range(96, 147)),
        "Neck": list(range(148, 165)),
        "Linker": list(range(166, 177)),
        "Head": list(range(178, 602)),
        'Receptor Contact': config['contact_sites'],
        "Total": list(range(71, 602)),
    }
    custom_order = ["Stalk", "Neck", "Linker", "Head", "Receptor Contact", "Total"]
    empty_charts = []
    for selection in df['selection'].unique():
        tmp_df = df[df["selection"] == selection]
        agg_means = []

        # For each barrel, filter the site_means dataframe to the sites belonging to that barrel and then store the means
        for barrel, sites in barrel_ranges.items():
            subset = tmp_df[tmp_df["site"].isin(sites)]
            for _, row in subset.iterrows():
                agg_means.append(
                    {"region": barrel, "binding_median": row["binding_median"], "site": row["site"]}
                )
            agg_means_df = pd.DataFrame(agg_means)

        chart = (
            alt.Chart(agg_means_df, title=f"{selection}")
            .mark_boxplot(color="darkgray", extent="min-max", opacity=1)
            .encode(
                x=alt.X(
                    "region:O",
                    sort=custom_order,
                    title="RBP Region",
                    axis=alt.Axis(labelAngle=-90),
                ),
                y=alt.Y(
                    "binding_median",
                    title=f"Binding",
                    axis=alt.Axis(grid=True, tickCount=4),
                ),
                tooltip=["region", "binding_median", "site"],
            ).properties(width=config['width'],height=config['height'])
        )
        empty_charts.append(chart)
    combined_effect_chart = alt.hconcat(*empty_charts).resolve_scale(
        y="shared", x="shared", color="independent"
    )
    return combined_effect_chart


entry_region_boxplot = make_boxplot_binding_region(df_binding_effect_concat)
entry_region_boxplot.display()
if entry_binding_combined_corr_plot is not None:
    entry_region_boxplot.save(binding_region_boxplot_plot)
In [20]:
def make_bubble_binding_region(df):
    barrel_ranges = {
        "Stalk": list(range(96, 147)),
        "Neck": list(range(148, 165)),
        "Linker": list(range(166, 177)),
        "Head": list(range(178, 602)),
        'Receptor Contact': config['contact_sites'],
        "Total": list(range(71, 602)),
    }
    custom_order = ["Stalk", "Neck", "Linker", "Head", "Receptor Contact", "Total"]
    empty_charts = []
    for selection in df['selection'].unique():
        tmp_df = df[df["selection"] == selection]
        agg_means = []

        # For each barrel, filter the site_means dataframe to the sites belonging to that barrel and then store the means
        for barrel, sites in barrel_ranges.items():
            subset = tmp_df[tmp_df["site"].isin(sites)]
            for _, row in subset.iterrows():
                agg_means.append(
                    {"region": barrel, "binding_median": row["binding_median"], "site": row["site"],"mutant": row["mutant"]}
                )
            agg_means_df = pd.DataFrame(agg_means)
        
        variant_selector = alt.selection_point(
        on="mouseover", empty=False, fields=["site",'mutant'], value=1
        )
        
        chart = (
            alt.Chart(agg_means_df, title=f"{selection}")
            .mark_point(opacity=0.3, stroke='black')
            .encode(
                x=alt.X(
                    "region:O",
                    sort=custom_order,
                    title="RBP Region",
                    axis=alt.Axis(labelAngle=-90),
                ),
                y=alt.Y(
                    "binding_median",
                    title=f"Binding",
                    axis=alt.Axis(grid=True, tickCount=4),
                ),
                xOffset="random:Q",
                tooltip=["region", "binding_median", "site","mutant"],
                color=alt.condition(
                    variant_selector, alt.value("orange"), alt.value("black")
                ),
                opacity=alt.condition(variant_selector, alt.value(1), alt.value(0.1)),
                strokeWidth=alt.condition(variant_selector,alt.value(2),alt.value(0)),
                size=alt.condition(variant_selector,alt.value(50),alt.value(15)),
                                          
            ).transform_calculate(
                random="sqrt(-1*log(random()))*cos(2*PI*random())"
            ).properties(width=config['width'],height=config['height'])
        ).add_params(variant_selector)
        empty_charts.append(chart)
    combined_effect_chart = alt.hconcat(*empty_charts).resolve_scale(
        y="shared", x="shared", color="independent"
    ).add_params(variant_selector)
    return combined_effect_chart


entry_region_bubble = make_bubble_binding_region(df_binding_effect_concat)
entry_region_bubble.display()
if entry_binding_combined_corr_plot is not None:
    entry_region_bubble.save(binding_region_bubble_plot)
In [ ]: